/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.enterprise.web;

import com.assg2.dao.DaoMovie;
import com.movie.Actor;
import com.movie.Movie;
import com.movie.Review;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sun.net.www.http.Hurryable;

/**
 *
 * @author Kevin
 */
public class ListSingleMovie extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           Movie movie = new Movie();
           int movieid = Integer.parseInt(request.getParameter("id"));
           DaoMovie dm = new DaoMovie();
           movie = (Movie)dm.findById(movieid);
           Set<Review> reviewSet = movie.getReviews();
           Set<Actor> actorSet = movie.getActors();
           ArrayList nicknameList = new ArrayList();
           ArrayList commentList = new ArrayList();
           ArrayList dateList = new ArrayList();
           ArrayList actorList = new ArrayList();
           Iterator i = actorSet.iterator();
           while(i.hasNext()){
               Actor actor = null;
               actor = (Actor)i.next();
               String name = actor.getFirstname()+" "+ actor.getLastname();
               actorList.add(name);
           }
           
           
           Iterator it = reviewSet.iterator();
           // sort by date
           ArrayList<Review> sortList = new ArrayList();
           
           while(it.hasNext()){
               Review review = (Review)it.next();
               sortList.add(review);
           }
           
           for(int j=0;j<sortList.size();j++){
               Review temp = null;
              // boolean isExchanged = false;
               for(int g=sortList.size()-1;g>j;g--){
                   if(sortList.get(g).getDateposted().before(sortList.get(g-1).getDateposted())){
                       temp = sortList.get(g);
                       sortList.set(g, sortList.get(g-1));
                       sortList.set(g-1, temp);
                   //    isExchanged = true;
                   }
                   
               }
//                  if (!isExchanged) {
//                    return;
//           }
           }
           
          for(int h=0;h<sortList.size();h++){
              Review r = (Review)sortList.get(h);
              String nickname = r.getViewer().getNickname();
               String comment = r.getDescription();
               Date date  = r.getDateposted();
               nicknameList.add(nickname);
               commentList.add(comment);
               dateList.add(date);
              
              
          }
           
//           while(it.hasNext()){
//               Review r = (Review)it.next();
//               String nickname = r.getViewer().getNickname();
//               String comment = r.getDescription();
//               Date date  = r.getDateposted();
//               nicknameList.add(nickname);
//               commentList.add(comment);
//               dateList.add(date);
//           }
           
           request.setAttribute("resultmovie", movie);
           request.setAttribute("nick", nicknameList);
           request.setAttribute("comment", commentList);
           request.setAttribute("date", dateList);
           request.setAttribute("actor", actorList);
           request.getRequestDispatcher("listSingleMovie.jsp").forward(request, response);
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
